<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            height: 100px;
            width: 100px;
            position: absolute;
            background-color: pink;
        }
        .box2{
            height: 150px;
            width: 150px;
            position: absolute;
            background-color: red;
            top: 120px;
        }
        .box3{
            height: 200px;
            width: 200px;
            position: absolute;
            background-color: yellow;
            top: 300px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <script>
        function DragBox(el) {
            this.el = document.querySelector(el);
        }
        DragBox.prototype = {
            constructor: DragBox,
            dragStar() {
                var self = this;
                this.el.onmousedown = function (evt) {
                    var e = evt || window.event;
                    var disX = e.pageX - this.offsetLeft;
                    var disY = e.pageY - this.offsetTop;
                    self.dragIng(disX, disY);
                    self.dragEnd()
                }

            },
            dragIng(x, y) {
                let self = this;
                document.onmousemove = function (evt) {
                    var e = evt || window.event;
                    self.el.style.left = e.pageX - x + "px";
                    self.el.style.top = e.pageY - y + "px";
                }
            },
            dragEnd() {
                document.onmouseup = function () {
                    document.onmousemove = document.onmouseup = null;
                }
            }
        }


        new DragBox(".box").dragStar()

        function DragBox2(el){
            this.el = document.querySelector(el);
        }
        DragBox2.prototype=new DragBox;
        DragBox2.prototype.dragIng=function(x,y){
            let self = this;
            document.onmousemove = function (evt) {
                var e = evt || window.event;
                let mx=e.pageX-x;
                let my=e.pageY-y;
                if(mx<=0){
                    mx=0;
                }
                if(my<=0){
                    my=0;
                }
                self.el.style.left =mx + "px";
                self.el.style.top =my + "px";
            }
        }

        new DragBox2(".box2").dragStar()
    
        function DragBox3(el){
            this.el = document.querySelector(el);
        }
        DragBox3.prototype=new DragBox;
        DragBox3.prototype.dragIng=function(x,y){
            let self = this;
            document.onmousemove = function (evt) {
                var e = evt || window.event;
                let mx=e.pageX-x;
                let my=e.pageY-y;
                if(mx<=0){
                    mx=0;
                }
                if(my<=0){
                    my=0;
                }
                self.el.style.left =mx + "px";
                self.el.style.top =my + "px";
                self.DragBoxLimitTitle(mx,my);
            }    
        }
        DragBox3.prototype.DragBoxLimitTitle=function(x,y){
            let self=this;
            this.el.innerHTML=`
            x:${x}px
            y:${y}px
            `
        }
    
        new DragBox3(".box3").dragStar()

    </script>
</body>

</html>